/*!
 * @brief       	初始化M24C32
 *
 * @param 				None
 *
 * @return				0/1: 错误/正确标志
 */
uint8_t Init_M24C32(void)
{
	uint8_t dat=0;
	uint32_t dat_32=0;
	uint8_t i=0;
	
	char strings[]="";
	int int_data[3]={0,0,0};
	uint8_t now_time_buf[8];
	uint8_t last_time_buf[8];
	uint8_t mid_time_buf[8];
	
	if(I2C_Judge(&hi2c1,M24C32_Mem_Slave_Add)!=true || I2C_Judge(&hi2c1,M24C32_Id_Slave_Add)!=true)
	{
		return 0;
	}
		
	dat_32=I2C_Read_y(&hi2c1,M24C32_Id_Slave_Add,0x0000,2,3,true);
	if(dat_32!=0x0020E00C)
	{
		return 0;
	}
	
	printf("[INFO] 开始Mem写入测试\n");
	dat=0;
	for(i=0;i<8;i++)
	{
		dat=0;
		I2C_Write_x(&hi2c1,M24C32_Mem_Slave_Add,0x0000+i,2,&dat,1,true);		
		if(I2C_Read_y(&hi2c1,M24C32_Mem_Slave_Add,0x0000+i,2,1,true)==0)
		{
			printf("[INFO] Mem 0x000%d 清空正确:	%02X\n",i,dat);
		}
		else
		{
			printf("[INFO] Mem 0x000%d 清空错误:	%02X\n",i,dat);
			printf("[INFO] Mem 清空中断\n");
			return 0;
		}
		dat=i+8;
		I2C_Write_x(&hi2c1,M24C32_Mem_Slave_Add,0x0000+i,2,&dat,1,true);
		if(I2C_Read_y(&hi2c1,M24C32_Mem_Slave_Add,0x0000+i,2,1,true)==i+8)
		{
			printf("[INFO] Mem 0x000%d 写入正确:	%02X\n",i,dat);
		}
		else
		{
			printf("[INFO] Mem 0x000%d 写入错误:	%02X\n",i,dat);
			printf("[INFO] Mem 写入中断\n");
			return 0;
		}
	}
	
	sscanf(now_date, "%s %d %d", strings, &int_data[0], &int_data[1]);
	int_data[2]=(strstr(month_names, strings)-month_names)/3+1;
	dat=(uint8_t)(int_data[1]/100);
	I2C_Write_x(&hi2c1,M24C32_Mem_Slave_Add,0x0000+0x08,2,&dat,1,true);		
	dat=(uint8_t)(int_data[1]%100);
	I2C_Write_x(&hi2c1,M24C32_Mem_Slave_Add,0x0000+0x09,2,&dat,1,true);	
	dat=(uint8_t)int_data[2];
	I2C_Write_x(&hi2c1,M24C32_Mem_Slave_Add,0x0000+0x0A,2,&dat,1,true);	
	dat=(uint8_t)int_data[0];
	I2C_Write_x(&hi2c1,M24C32_Mem_Slave_Add,0x0000+0x0B,2,&dat,1,true);

	sscanf(now_time, "%d:%d:%d",&int_data[0], &int_data[1],&int_data[2]);
	dat=(uint8_t)int_data[0];
	I2C_Write_x(&hi2c1,M24C32_Mem_Slave_Add,0x0000+0x0C,2,&dat,1,true);		
	dat=(uint8_t)int_data[1];
	I2C_Write_x(&hi2c1,M24C32_Mem_Slave_Add,0x0000+0x0D,2,&dat,1,true);	
	dat=(uint8_t)int_data[2];
	I2C_Write_x(&hi2c1,M24C32_Mem_Slave_Add,0x0000+0x0E,2,&dat,1,true);	
	dat=1;
	I2C_Write_x(&hi2c1,M24C32_Mem_Slave_Add,0x0000+0x0F,2,&dat,1,true);
	
	for(i=0;i<8;i++)
	{
		now_time_buf[i]=I2C_Read_y(&hi2c1,M24C32_Mem_Slave_Add,0x0000+0x08+i,2,1,true);		
		mid_time_buf[i]=I2C_Read_y(&hi2c1,M24C32_Mem_Slave_Add,0x0000+0x10+i,2,1,true);
	}
	
	printf("[INFO] 写入当前编译时间：%02d%02d/%02d/%02d %02d:%02d:%02d\n",
		now_time_buf[0],
		now_time_buf[1],
		now_time_buf[2],
		now_time_buf[3],
		now_time_buf[4],
		now_time_buf[5],
		now_time_buf[6]);
	
	if( now_time_buf[0]!=mid_time_buf[0] || 
			now_time_buf[1]!=mid_time_buf[1] || 
			now_time_buf[2]!=mid_time_buf[2] || 
			now_time_buf[3]!=mid_time_buf[3] || 
			now_time_buf[4]!=mid_time_buf[4] || 
			now_time_buf[5]!=mid_time_buf[5] || 
			now_time_buf[6]!=mid_time_buf[6] )
	{
		for(i=0;i<8;i++)
		{
			dat=mid_time_buf[i];
			I2C_Write_x(&hi2c1,M24C32_Mem_Slave_Add,0x0000+0x18+i,2,&dat,1,true);	
			dat=now_time_buf[i];
			I2C_Write_x(&hi2c1,M24C32_Mem_Slave_Add,0x0000+0x10+i,2,&dat,1,true);
		}
	}
	else
	{
		printf("[INFO] 上次运行后未重新编译\n");
	}
	
	if(I2C_Read_y(&hi2c1,M24C32_Mem_Slave_Add,0x0000+0x1F,2,1,true)==1)
	{
		for(i=0;i<8;i++)
		{
			last_time_buf[i]=I2C_Read_y(&hi2c1,M24C32_Mem_Slave_Add,0x0000+0x18+i,2,1,true);
		}
		printf("[INFO] 上次编译时间：%02d%02d/%02d/%02d %02d:%02d:%02d\n",
			last_time_buf[0],
			last_time_buf[1],
			last_time_buf[2],
			last_time_buf[3],
			last_time_buf[4],
			last_time_buf[5],
			last_time_buf[6]);
	}
	else
	{
		printf("[INFO] 当前编译时间：%s %s\n",now_date,now_time);
		sscanf(now_date, "%s %d %d", strings, &int_data[0], &int_data[1]);
		int_data[2]=(strstr(month_names, strings)-month_names)/3+1;
		dat=(uint8_t)(int_data[1]/100);
		I2C_Write_x(&hi2c1,M24C32_Mem_Slave_Add,0x0000+0x18,2,&dat,1,true);		
		dat=(uint8_t)(int_data[1]%100);
		I2C_Write_x(&hi2c1,M24C32_Mem_Slave_Add,0x0000+0x19,2,&dat,1,true);	
		dat=(uint8_t)int_data[2];
		I2C_Write_x(&hi2c1,M24C32_Mem_Slave_Add,0x0000+0x1A,2,&dat,1,true);	
		dat=(uint8_t)int_data[0];
		I2C_Write_x(&hi2c1,M24C32_Mem_Slave_Add,0x0000+0x1B,2,&dat,1,true);

		sscanf(now_time, "%d:%d:%d",&int_data[0], &int_data[1],&int_data[2]);
		dat=(uint8_t)int_data[0];
		I2C_Write_x(&hi2c1,M24C32_Mem_Slave_Add,0x0000+0x1C,2,&dat,1,true);		
		dat=(uint8_t)int_data[1];
		I2C_Write_x(&hi2c1,M24C32_Mem_Slave_Add,0x0000+0x1D,2,&dat,1,true);	
		dat=(uint8_t)int_data[2];
		I2C_Write_x(&hi2c1,M24C32_Mem_Slave_Add,0x0000+0x1E,2,&dat,1,true);	
		dat=1;
		I2C_Write_x(&hi2c1,M24C32_Mem_Slave_Add,0x0000+0x1F,2,&dat,1,true);
		
		printf("[INFO] 写入当前编译时间至上次编译时间：%02d%02d/%02d/%02d %02d:%02d:%02d\n",
			I2C_Read_y(&hi2c1,M24C32_Mem_Slave_Add,0x0000+0x18,2,1,true),
			I2C_Read_y(&hi2c1,M24C32_Mem_Slave_Add,0x0000+0x19,2,1,true),
			I2C_Read_y(&hi2c1,M24C32_Mem_Slave_Add,0x0000+0x1A,2,1,true),
			I2C_Read_y(&hi2c1,M24C32_Mem_Slave_Add,0x0000+0x1B,2,1,true),
			I2C_Read_y(&hi2c1,M24C32_Mem_Slave_Add,0x0000+0x1C,2,1,true),
			I2C_Read_y(&hi2c1,M24C32_Mem_Slave_Add,0x0000+0x1D,2,1,true),
			I2C_Read_y(&hi2c1,M24C32_Mem_Slave_Add,0x0000+0x1E,2,1,true));
	}
		
	return 1;
}
